package com.huat.sportmeetingmanagement.controller;

import com.github.pagehelper.PageInfo;
import com.huat.sportmeetingmanagement.entity.GeneralVO;
import com.huat.sportmeetingmanagement.entity.Project;
import com.huat.sportmeetingmanagement.entity.Record;
import com.huat.sportmeetingmanagement.entity.enumEntity.ErrorListEnum;
import com.huat.sportmeetingmanagement.service.ProjectService;
import com.huat.sportmeetingmanagement.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("record")
public class RecordController {
    @Autowired
    RecordService recordService;
    @Autowired
    ProjectService projectService;
    @PostMapping("/getRecordById")
    public GeneralVO getRecordById(@RequestBody Record record){
        Record Record = recordService.getRecordById(record);
        return new GeneralVO(ErrorListEnum.E200,Record);

    }

    @PostMapping("/getRecords")
    public GeneralVO getRecords(@RequestBody Record record){
        List<Record> Records = recordService.getRecords(record);
        PageInfo<Record> RecordPageInfo = new PageInfo<>(Records);
        return new GeneralVO(ErrorListEnum.E200,RecordPageInfo);
    }
    @PostMapping("/updateRecord")
    public GeneralVO updateRecord(@RequestBody Record record){
        Integer result = 0;
        if (recordService.getRecordById(record).getProjectStatus()!=2)
            return new GeneralVO(ErrorListEnum.E20007,"该比赛未结束无法录入成绩");
        else
            result = recordService.updateRecord(record);
        return new GeneralVO(ErrorListEnum.E200,result);
    }
    @PostMapping("/addRecord")
    public GeneralVO addRecord(@RequestBody Record record){
        Integer result = 0;
        try{
            Project project = projectService.getProjectByNo(record.getProjectNo());
            if(project.getProjectMaxNum()==project.getProjectNum())
                return new GeneralVO(ErrorListEnum.E20007,"该项目报名人数已满");
            else
                result = recordService.addRecord(record);}
        catch (Exception ex){
            return new GeneralVO(ErrorListEnum.E60001,"无法重复报名");
        }
        return new GeneralVO(ErrorListEnum.E200,result);
    }
    @PostMapping("/deleteRecord")
    public GeneralVO deleteRecord(@RequestBody Record record){
        Integer result = 0;
        result = recordService.deleteRecord(record);
        return new GeneralVO(ErrorListEnum.E200,result);
    }

}